package com.example.su_shi_tan_main

import android.os.Bundle
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.example.library_common.utils.normalShortToast
import com.example.su_shi_tan_bas_lib.base.BaseFragmentEx
import com.example.su_shi_tan_bas_lib.utils.even.MessageWrap
import com.example.su_shi_tan_bas_lib.utils.even.USER_LOGInED
import com.example.su_shi_tan_bas_lib.view.bottombar.BottomBar
import com.example.su_shi_tan_bas_lib.view.bottombar.BottomBarTab
import com.example.su_shi_tan_core.arouter.*
import com.example.su_shi_tan_core.db.LogInTokenManager
import com.example.su_shi_tan_core.extentions.castBaseFragmentEx
import com.example.su_shi_tan_core.extentions.startSuperFragmentDoNotHideSelf
import com.example.su_shi_tan_core.getUserInfo
import com.example.su_shi_tan_main.bean.LabInfo
import java.util.*
import kotlin.math.abs

@Route(path = MainARouter.Main_Fragment)
class MainFragment : BaseFragmentEx() {

    private var mBottomBar: BottomBar? = null

    private var isLoadSubPage = false

    private var childFragment = childPage()

    private var lastBackPress = 0

    override fun getLayoutId(): Any {
        return R.layout.main_fragment
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)


        mBottomBar = view.findViewById(R.id.bottomBar)

        if (!isLoadSubPage) {
            loadSubPage()
            isLoadSubPage = true
        }

    }

    override fun onSupportVisible() {
        super.onSupportVisible()
        if (LogInTokenManager.tokenManager.haveSignedIn()) {
            getUserInfo(_mActivity)
        }
    }

    override fun handMessage(msg: MessageWrap?) {
        super.handMessage(msg)
        when(msg?.message){
            MineARouter.ADD_DELIVERY_INFO,USER_LOGInED->{
                if (LogInTokenManager.tokenManager.haveSignedIn()) {
                    getUserInfo(_mActivity)
                }
            }
        }
    }

    private fun loadSubPage() {
        val pages = mutableListOf<BaseFragmentEx>()
        childFragment.forEachIndexed { _, it ->
//            val bottomBor = it as BottomBorInfo
            val page = it.childFragment
            pages.add(page)
            mBottomBar?.addItem(
                BottomBarTab(_mActivity)
                    .setPageTag(page)
                    .setContent(it.title, it.icon)
            )
        }

        loadMultipleRootFragment(R.id.fl_container, 0, *pages.toTypedArray())

        mBottomBar?.setOnTabSelectedListener(object : BottomBar.OnTabSelectedListener {
            override fun onTabReselected(position: Int) {
                val nextFragment = childFragment[position].childFragment
                nextFragment.refreshData()

                if (LogInTokenManager.tokenManager.haveSignedIn()) {
                    getUserInfo(_mActivity)
                }
//                val mBottomBorInfo = nextFragment as BottomBorInfo

//                mBottomBorInfo.refreshData()
            }

            override fun onTabUnselected(position: Int) {

            }

            override fun onBeforeSelected(position: Int): Boolean {
                val labInfo = childFragment[position]

                if (!labInfo.openPage) {
                    normalShortToast(_mActivity,"暂未开放")
                    return false
                }

                val needLogin = labInfo.needLogin
                if (needLogin && !LogInTokenManager.tokenManager.haveSignedIn()) {
                    startLogin()
                    return false
                }
                return true
            }

            override fun onTabSelected(position: Int, prePosition: Int) {
                val nextFragment = childFragment[position].childFragment

                showHideFragment(nextFragment, childFragment[prePosition].childFragment)

//                val mBottomBorInfo = nextFragment as BottomBorInfo

//                mBottomBorInfo.refreshData()
            }
        })
    }

    private fun startLogin() {
        ARouter.getInstance()
            .build(SignInARouter.SignInFragment)
            .startSuperFragmentDoNotHideSelf(this)
    }

    override fun onBackPressedSupport(): Boolean {
        val currentTimeMillis = System.currentTimeMillis().toInt()
        if (abs(currentTimeMillis - lastBackPress) > 1000) {
            lastBackPress = currentTimeMillis
            normalShortToast(_mActivity, R.string.exit_app)
//            Toast.makeText(_mActivity,  , Toast.LENGTH_SHORT).show()


        } else {
            _mActivity.finish()
        }

        return true
    }
}